package com.zb.springcloud.Untils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;

import java.util.Map;
import java.util.Optional;


public class OpenStackHttpUtil {
    /**
     * 默认超时时间为30秒
     */
    private static final Integer DEFAULT_HTTP_TIME_OUT = 30000;

    public static HttpResponse post(String url, Map<String, String> headers, String body) {
        return OpenStackHttpUtil.post(url, headers, body, DEFAULT_HTTP_TIME_OUT);
    }

    public static HttpResponse post(String url, Map<String, String> headers, String body, Integer timeout) {
        HttpRequest request = HttpUtil.createRequest(Method.POST, url).charset(CharsetUtil.CHARSET_UTF_8);
        request.contentType("application/json");
        Optional.ofNullable(headers).ifPresent(h -> {
            h.forEach(request::header);
        });
        Optional.ofNullable(body).ifPresent(request::body);
        return request.timeout(timeout).execute();
    }

    public static HttpResponse get(String url, Map<String, String> headers, Map<String, Object> params) {
        return OpenStackHttpUtil.get(url, headers, params, DEFAULT_HTTP_TIME_OUT);
    }

    public static HttpResponse get(String url, Map<String, String> headers, Map<String, Object> params, Integer timeout) {
        HttpRequest request = HttpUtil.createRequest(Method.GET, url).charset(CharsetUtil.CHARSET_UTF_8);
        request.contentType("application/json");
        Optional.ofNullable(headers).ifPresent(h -> {
            h.forEach(request::header);
        });
        Optional.ofNullable(params).ifPresent(p -> {
            request.form(params);
        });
        return request.timeout(timeout).execute();
    }

    public static HttpResponse put(String url, Map<String, String> headers, Map<String, Object> params) {
        return OpenStackHttpUtil.put(url, headers, params, DEFAULT_HTTP_TIME_OUT);
    }

    public static HttpResponse put(String url, Map<String, String> headers, Map<String, Object> params, Integer timeout) {
        HttpRequest request = HttpUtil.createRequest(Method.PUT, url).charset(CharsetUtil.CHARSET_UTF_8);
        request.contentType("application/json");
        Optional.ofNullable(headers).ifPresent(h -> {
            h.forEach(request::header);
        });
        Optional.ofNullable(params).ifPresent(p -> {
            String body = JSON.toJSONString(p);
            request.body(body);
        });
        return request.timeout(timeout).execute();
    }

    public static HttpResponse put(String url, Map<String, String> headers, String body) {
        return OpenStackHttpUtil.put(url, headers, body, DEFAULT_HTTP_TIME_OUT);
    }

    public static HttpResponse put(String url, Map<String, String> headers, String body, Integer timeout) {
        HttpRequest request = HttpUtil.createRequest(Method.PUT, url).charset(CharsetUtil.CHARSET_UTF_8);
        request.contentType("application/json");
        Optional.ofNullable(headers).ifPresent(h -> {
            h.forEach(request::header);
        });
        Optional.ofNullable(body).ifPresent(request::body);
        return request.timeout(timeout).execute();
    }

    public static HttpResponse delete(String url, Map<String, String> headers, Map<String, Object> params) {
        return OpenStackHttpUtil.delete(url, headers, params, DEFAULT_HTTP_TIME_OUT);
    }

    public static HttpResponse delete(String url, Map<String, String> headers, Map<String, Object> params, Integer timeout) {
        HttpRequest request = HttpUtil.createRequest(Method.DELETE, url).charset(CharsetUtil.CHARSET_UTF_8);
        request.contentType("application/json");
        Optional.ofNullable(headers).ifPresent(h -> {
            h.forEach(request::header);
        });
        Optional.ofNullable(params).ifPresent(p -> {
            request.form(params);
        });
        return request.timeout(timeout).execute();
    }

    public static HttpResponse patch(String url, Map<String, String> headers, String body) {
        return OpenStackHttpUtil.patch(url, headers, body, DEFAULT_HTTP_TIME_OUT);
    }

    public static HttpResponse patch(String url, Map<String, String> headers, String body, Integer timeout) {
        HttpRequest request = HttpUtil.createRequest(Method.PATCH, url).charset(CharsetUtil.CHARSET_UTF_8);
        request.contentType("application/json");
        Optional.ofNullable(headers).ifPresent(h -> {
            h.forEach(request::header);
        });
        Optional.ofNullable(body).ifPresent(request::body);
        return request.timeout(timeout).execute();
    }
}
